#include "allincludes.h"

OneLine Container::Average(int indexStart, int indexEnd)
{
	//cout<<"indexStart/End "<<indexStart<<" "<<indexEnd<<endl;
	if( indexStart<0 || indexEnd>usedSize-1 )
	{
		cout<<"Error : Doing Wrong Average... "<<endl;
		exit (FAILED);
	}

	if(indexStart>=indexEnd)
	{
		return lineList[indexStart];
	}
	else
	{
		OneLine newLine(lineList[indexStart]);
		for(int j=indexStart+1;j<=indexEnd;j++)
		{
			newLine = newLine+ lineList[j];
		}
		newLine =newLine / (indexEnd-indexStart+1);

		return newLine;
	}
	
/*
{
	//cout<<"indexStart/End "<<indexStart<<" "<<indexEnd<<endl;
	//Not completed yet, many vars are ignored
	OneLine newLine(*lineList);
	if(indexStart>=indexEnd)
	{
		return newLine;
	}
	else
	{

		for(int j=indexStart+1;j<=indexEnd;j++)
		{
			
			
			for(int i=0;i<newLine.binsNum;i++)
			{
				newLine.data[i]+=(lineList+j)->data[i];
			}	
			for(int k=0;k<newLine.iNumberOfAnalog;k++)
			{
				newLine.analogData[k]+=(lineList+j)->analogData[k];
			}
			//Well, remember not to divide the averageTime
			newLine.averageTime+=(lineList+j)->averageTime;
			newLine.lNumberOfShotsPerAverage+=(lineList+j)->lNumberOfShotsPerAverage;
				
		}
		for(int i=0;i<newLine.binsNum;i++)
		{
			newLine.data[i]/=(indexEnd-indexStart+1);
		}
		for(int k=0;k<newLine.iNumberOfAnalog;k++)
		{
			newLine.analogData[k]/=(indexEnd-indexStart+1);
		}
		
		return newLine;
	}*/
}
int Container::SetData(int index, OneLine newLine)
{
	if(index<0 || index>usedSize-1)
	{
		cout<<"Error: Container can't set data to list pos: "<<index<<endl;
		exit(0);
	}
	lineList[index]=newLine;
	return SUCCEED;
}
int Container::AddData(OneLine newLine)
{
	if(usedSize>=maxSize)
	{
		cout<<"Container list full!!"<<endl;
		exit(0);
	}
	usedSize++;
	lineList[usedSize-1]=newLine;
	return SUCCEED;
}
OneLine Container::GetData(int index)
{
	if(index<0 || index>usedSize-1)
	{
		cout<<"Error: Can't Get Data from Container"<<endl;
		exit(0);
	}
	OneLine temp;
	temp=lineList[index];
	return temp;
}
int Container::DeleteData(int index)
{
	if(index<0 || index>usedSize-1)
	{
		cout<<"Error: Deleting wrong data in Container"<<endl;
		exit(0);
	}
	for(int i=index;i<usedSize-2;i++)
	{
		lineList[i]=lineList[i+1];
	}
	usedSize--;
	return SUCCEED;
}
int Container::ClearAll()
{
	usedSize=0;
	return SUCCEED;
}
int Container::InsertData(int index, OneLine newLine)
{
	if(usedSize>=maxSize)
	{
		cout<<"Container list full!!"<<endl;
		exit(0);
	}	
	for(int i=usedSize-1; i>=index;i--)
	{
		lineList[i+1]=lineList[i];
	}
	usedSize++;
	lineList[index]=newLine;
	return SUCCEED;
}

